package com.inkrl.user.controller;

import com.inkrl.user.enums.ResCode;
import com.inkrl.user.service.TokenService;
import com.jt.common.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

@RestController
@CrossOrigin
@RequestMapping("/token")
public class TokenController {
    @Autowired 
    TokenService tokenService;
    
    /**
     * @author InkReturn
     * @Description : 检查前端发送来的token
     * @date: 2020/9/14 10:00
     * @methodName getUserByToken
     * @param token  token令牌
     * @return Json
     * {
     * uname:(x),
     * resCode:(x)
     * }
     */
    @ResponseBody
    @RequestMapping(value = "/user",method = RequestMethod.GET)
    public SysResult getUserByToken(@RequestHeader String token){
        HashMap<String, String> responseMap = new HashMap<String, String>();
        //调用tokenService获取token
        String userJson=tokenService.getUserJsonByToken(token);
        if(userJson!=null) {
            //获取成功返回token
            responseMap.put("msg","用户已登录");
            responseMap.put("resCode", ResCode.OK);
            responseMap.put("user",userJson);
            return SysResult.build(200,"请求完成",responseMap);
        }
        //获取失败返回token过期的信息
        responseMap.put("msg","token不存在或者已经过时");
        responseMap.put("resCode", ResCode.TOKEN_NOT_GET);
        return SysResult.build(200,"请求完成",responseMap);
    }
}
